package top.lixunda.ecommerce.server.good.api.entity.vo;

import io.swagger.annotations.ApiModel;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
import top.lixunda.common.utils.ObjectValidators;
import top.lixunda.ecommerce.server.good.api.entity.db.*;

import java.util.*;

/**
 * 订单评论详细信息
 *
 * @author lixunda
 * @version time:2020/3/7 12:19
 */
@ApiModel("订单评论详细信息")
@Data
@EqualsAndHashCode(callSuper = true)
@Accessors(chain = true)
public class OrderReviewConvertVO extends OrderReviewDO {

    /**
     * 评论图片
     */
    private List<OrderReviewImageVO> imageList;

    /**
     * 评论追加评论数据
     */
    private List<OrderReviewMoreVO> moreReviewList;

    /**
     * 数据转换
     *
     * @param detailList 订单评论详细信息DO
     * @return 转换后的信息
     */
    @SuppressWarnings("ConstantConditions")
    public static List<OrderReviewConvertVO> fromOrderReviewDetailList(List<OrderReviewDetailDO> detailList) {
        if (detailList == null) {
            return null;
        }
        if (detailList.size() == 0) {
            return Collections.emptyList();
        }
        // 保证顺序
        Set<Integer> dtoSet = new LinkedHashSet<>(10);
        Map<Integer, OrderReviewConvertVO> orderReviewCache = new HashMap<>(detailList.size());
        Map<Integer, OrderReviewMoreVO> orderReviewMoreCache = new HashMap<>(detailList.size());
        // 防重复
        Map<Integer, Integer> picture = new HashMap<>(detailList.size());
        Map<Integer, Integer> moreReview = new HashMap<>(detailList.size());
        Map<Integer, Integer> morePicture = new HashMap<>(detailList.size());
        OrderReviewConvertVO temp;
        for (OrderReviewDetailDO detail : detailList) {
            temp = orderReviewCache.get(detail.getId());
            if (temp == null) {
                temp = convertSimpleData(detail);
                temp.setImageList(new ArrayList<>(8));
                temp.setMoreReviewList(new ArrayList<>(8));
                dtoSet.add(temp.getId());
                orderReviewCache.put(temp.getId(), temp);
            }
            // 图片
            if (detail.getImageId() != null &&
                    picture.get(detail.getImageId()) == null) {
                OrderReviewImageVO orderReviewImageVO = convertReviewImage(detail);
                if (ObjectValidators.isNotEmpty(orderReviewImageVO)) {
                    temp.getImageList().add(orderReviewImageVO);
                }
                picture.put(detail.getImageId(), 1);
            }
            // 追加评论
            if (detail.getOrderReviewMoreId() != null &&
                    moreReview.get(detail.getOrderReviewMoreId()) == null) {
                OrderReviewMoreVO orderReviewMoreVO = convertMoreReview(detail);
                if (ObjectValidators.isNotEmpty(orderReviewMoreVO)) {
                    orderReviewMoreVO.setImageList(new ArrayList<>(8));
                    temp.getMoreReviewList().add(orderReviewMoreVO);
                    orderReviewMoreCache.put(detail.getOrderReviewMoreId(), orderReviewMoreVO);
                }
                moreReview.put(detail.getOrderReviewMoreId(), 1);
            }
            // 追加评论图片
            if (detail.getMoreImageId() != null &&
                    morePicture.get(detail.getMoreImageId()) == null) {
                OrderReviewMoreImageVO orderReviewMoreImageVo = convertMoreImage(detail);
                if (ObjectValidators.isNotEmpty(orderReviewMoreImageVo)) {
                    orderReviewMoreCache.get(detail.getOrderReviewMoreId())
                            .getImageList().add(orderReviewMoreImageVo);
                }
                morePicture.put(detail.getOrderReviewMoreId(), 1);
            }
        }
        List<OrderReviewConvertVO> list = new ArrayList<>(dtoSet.size());
        for (Integer orderId : dtoSet) {
            list.add(orderReviewCache.get(orderId));
        }
        return list;
    }

    /**
     * 数据转换
     *
     * @param detail OrderReviewDetailDO
     * @return OrderReviewConvertVO
     */
    private static OrderReviewConvertVO convertSimpleData(OrderReviewDetailDO detail) {
        if (detail == null || detail.getId() == null) {
            return null;
        }
        OrderReviewConvertVO vo = new OrderReviewConvertVO();
        vo.setId(detail.getId())
                .setUserId(detail.getUserId())
                .setUserName(detail.getUserName())
                .setStoreId(detail.getStoreId())
                .setStoreName(detail.getStoreName())
                .setOrderId(detail.getOrderId())
                .setGradeDesc(detail.getGradeDesc())
                .setGradeLogis(detail.getGradeLogis())
                .setGradeService(detail.getGradeService())
                .setContent(detail.getContent())
                .setAnonymousEnable(detail.getAnonymousEnable())
                .setTime(detail.getTime());
        return vo;
    }

    /**
     * 数据转换
     *
     * @param detail OrderReviewDetailDO
     * @return OrderReviewImageDO
     */
    private static OrderReviewImageVO convertReviewImage(OrderReviewDetailDO detail) {
        if (detail == null || detail.getImageId() == null) {
            return null;
        }
        OrderReviewImageVO vo = new OrderReviewImageVO();
        vo.setId(detail.getImageId())
                .setOrderReviewId(detail.getId())
                .setImage(detail.getImage())
                .setFileId(detail.getImageFileId())
                .setCreateTime(detail.getImageCreateTime());
        return vo;
    }

    /**
     * 数据转换
     *
     * @param detail OrderReviewDetailDO
     * @return OrderReviewMoreVO
     */
    private static OrderReviewMoreVO convertMoreReview(OrderReviewDetailDO detail) {
        if (detail == null || detail.getOrderReviewMoreId() == null) {
            return null;
        }
        OrderReviewMoreVO vo = new OrderReviewMoreVO();
        vo.setId(detail.getOrderReviewMoreId())
                .setOrderReviewId(detail.getId())
                .setUserId(detail.getUserId())
                .setUserName(detail.getUserName())
                .setContent(detail.getMoreContent())
                .setTime(detail.getMoreTime());
        return vo;
    }

    /**
     * 数据转换
     *
     * @param detail OrderReviewDetailDO
     * @return OrderReviewMoreImageDO
     */
    private static OrderReviewMoreImageVO convertMoreImage(OrderReviewDetailDO detail) {
        if (ObjectValidators.isEmpty(detail) || detail.getMoreImageId() == null) {
            return null;
        }
        OrderReviewMoreImageVO vo = new OrderReviewMoreImageVO();
        vo.setId(detail.getMoreImageId())
                .setOrderReviewMoreId(detail.getOrderReviewMoreId())
                .setImage(detail.getMoreImage())
                .setFileId(detail.getMoreImageFileId())
                .setCreateTime(detail.getMoreImageCreateTime());
        return vo;
    }
}
